/**
 * 
 */
package com.k.demo.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.k.demo.entity.GoodOrder;

/**
 * @author MSI-PC
 *
 */

@Mapper
public interface GoodOrderMapper {
	
	//获取全部用户的全部订单
	@Select("select * from good_order where is_delete = 1 order by order_create_time DESC")
	List<GoodOrder> getAllGoodOrders();
	
	//获取当前用户的全部订单
	@ResultMap("GoodOrderWithCountMap")
	@Select("select * from good_order where order_owner=#{user_id} and is_delete = 1 order by order_create_time DESC")
	List<GoodOrder> getUserAllGoodOrders(Integer user_id);
	
	//获取当前用户的已取消
	@Select("select * from good_order where order_owner=#{user_id} and stutus=11 and is_delete = 1 order by order_create_time DESC")
	List<GoodOrder> getUserCancelGoodOrders(Integer user_id);
	
	//获取当前用户的已支付
	@Select("select * from good_order where order_owner=#{user_id} and stutus=12 and is_delete = 1 order by order_create_time DESC")
	List<GoodOrder> getUserPaidGoodOrders(Integer user_id);
	
	//获取当前用户的已送达
	@Select("select * from good_order where order_owner=#{user_id} and stutus=13 and is_delete = 1 order by order_create_time DESC")
	List<GoodOrder> getUserArrivedGoodOrders(Integer user_id);
	
	//生成订单并返回订单id
	boolean insertGoodOrder(GoodOrder goodOrder);
	
	//根据订单id查询
	GoodOrder getGoodOrderById(int orderid);
	
	//更新订单数据
	int updateGoodOrder(GoodOrder goodOrder);
	
	@Update("update good_order set status= 13 where order_id=#{order_id}")
	int updateGoodOrderState(int order_id);
	
	//更新订单状态与当前时间比较
	int updateGoodOrderByDate(Integer user_id);
	
	//根据传入的两个时间进行查询
	List<GoodOrder> getGoodOrderByTime(@Param("time1")String time1,@Param("time2")String time2);
	
	//批量删除
	boolean deleteByGroup(List<Integer> list);
	
	//根据id假删除
	@Update("Update good_order set is_delete=0 where order_id=#{order_id} and is_delete =1")
	int falseDeleteById(int order_id);
	

	//根据当天天数查询记录数
	@Select("select count(*) from good_order where trunc(order_create_time)=to_date(#{date},'yyyy-mm-dd') and is_delete=1")
	int selectCountByDate(String date);
	
	//根据时间段来查询记录数 根据日期分组
	@Select("select to_char(order_create_time,'yyyy-mm-dd') from good_order where order_create_time>=to_date(#{start_date},'yyyy-mm-dd hh24:mi:ss') and order_create_time<=to_date(#{end_date},'yyyy-mm-dd hh24:mi:ss')+1  and  is_delete=1 group by to_char(order_create_time,'yyyy-mm-dd')")
	List<String> selectByTime(String start_date,String end_date);
	
	//获得当天订单数
	@Select("select count(*) from good_order where trunc(order_create_time)=trunc(sysdate) and is_delete = 1")
	int selectCountToday();
	
	//获取时间段内的订单数
	@ResultMap("GoodOrderWithTotalNumberMap")
	@Select("select nvl(count(*),0),to_char(order_create_time,'yyyy-mm-dd') from good_order where trunc(order_create_time)>=trunc(to_date(#{start_date},'yyyy-mm-dd hh24:mi:ss')) and trunc(order_create_time)<=trunc(to_date(#{end_date},'yyyy-mm-dd hh24:mi:ss')+1) and is_delete=1 group by to_char(order_create_time,'yyyy-mm-dd')")
	List<GoodOrder> selectCountByTime(String start_date,String end_date);
	
	//获取时间段内订单的总价格和实际价格
	@ResultMap("GoodOrderWithTotalPriceMap")
	@Select("select nvl(sum(order_original_price),0),nvl(sum(order_true_price),0),to_char(order_create_time,'yyyy-mm-dd') from good_order where trunc(order_create_time)>=trunc(to_date(#{start_date},'yyyy-mm-dd hh24:mi:ss')) and trunc(order_create_time)<=trunc(to_date(#{end_date},'yyyy-mm-dd hh24:mi:ss')+1) and is_delete=1 group by to_char(order_create_time,'yyyy-mm-dd')")
	List<GoodOrder> selectOriginAndActrulPriceByTime(String start_date,String end_date);
	
	
	//订单组合查询
	List<GoodOrder> getByGoodOrderInfo(GoodOrder goodOrder);
	
	//获得一个连续的时间的日期数组
	@Select("select to_char(to_date(#{start_date}, 'yyyy-mm-dd') + rownum - 1,'yyyy-mm-dd')\r\n" + 
			"     from dual\r\n" + 
			"   connect by rownum <= to_date(#{end_date}, 'yyyy-mm-dd') -\r\n" + 
			"              to_date(#{start_date}, 'yyyy-mm-dd') + 1")
	List<String> getDate(String start_date,String end_date);
	
	//获取当天销售总额
	@Select("select nvl(sum(order_original_price),0) from good_order where trunc(order_create_time)=trunc(sysdate) and is_delete = 1 ")
	Double selectTotalSales();
	//
	@Select("select nvl(sum(order_true_price),0) from good_order where trunc(order_create_time)=trunc(sysdate) and is_delete = 1")
	Double selectActualSales();
	
	//获取当天销售总额
	@Select("select nvl(sum(order_original_price),0) from good_order where trunc(order_create_time)=to_date(#{date},'yyyy-mm-dd') and is_delete = 1")
	Double selectTotalSalesByDate(String date);
	//
	@Select("select nvl(sum(order_true_price),0) from good_order where trunc(order_create_time)=to_date(#{date},'yyyy-mm-dd') and is_delete = 1")
	Double selectActualSalesByDate(String date);	
	
	//根据传入的用户id获取订单的数量
	@Select("select count(order_id) from good_order where order_owner=#{user_id} and is_delete = 1")
	int getAllOrderAmountByUserId(int user_id);
	
	//获取待评价的订单数量
	@Select("select count(order_id) from good_order where order_owner=#{user_id} and status=17 and is_delete = 1")
	int getNotEvaluationOrderAmountByUserId(int user_id);

}
